package com.kgd.news.ibeetl.func;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.kgd.news.utils.Constants;
import org.beetl.core.Context;
import org.beetl.core.Function;

import java.util.List;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/8/3
 * @Time: 下午2:47
 */
public class OneToManyDictDisplayFunc implements Function {

    @Override
    public Object call(Object[] objects, Context context) {
        try {
            if (objects.length != 4) {
                return "参数错误";
            }
            String id = objects[0] + "";
            String tblName = objects[1] + "";
            String colName = objects[2] + "";
            String dgroup = objects[3] + "";
            List<Record> data = Db.findByCache(Constants.dict_func_cacheName,"onetomanydictdisplayfunc"+id+dgroup,"select " + colName + " from " + tblName + " where d_value = ? and d_group =? ", id,dgroup);
            if (data == null && data.isEmpty()) {
                return "无";
            } else {
                return data.get(0).get(colName);
            }
        } catch (Exception e) {
            return "暂无";
        }
    }
}
